package com.eshin.thymeleaf.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
public class TestController {
	
	@ModelAttribute("allFeatures")
    public List<String> populateFeatures() {
		List<String>list = new ArrayList<>(3);
		list.add("aaaa");
		list.add("bbbb");
		list.add("cccc");
        return list;
    }
	@RequestMapping(value="/test")
	public String test(Model model,@RequestParam(value="pageIndex",defaultValue="1") Integer pageIndex){
		List<Map<String,Object>> items = new ArrayList<Map<String,Object>>();
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("id", "1");
		map.put("name", "zhangsan");
		map.put("mobile", "15038074114");
		map.put("money", 7.52);
		map.put("createTime","2014-11-21 10:09:10");
		items.add(map);
		map = new HashMap<String, Object>();
		map.put("id", "2");
		map.put("name", "lisi");
		map.put("mobile", "");
		map.put("money", 12.52);
		map.put("createTime","2014-12-11 10:09:10");
		items.add(map);
		map = new HashMap<String, Object>();
		map.put("id", "3");
		map.put("name", "wangwu");
		map.put("mobile", "13038074114");
		map.put("money", 18.2);
		map.put("createTime",null);
		items.add(map);
		model.addAttribute("items", items);
		Page page = new Page();
		page.setItems(items);
		page.setTotal(42);
		page.setLastPage(true);
		page.setPageIndex(pageIndex);
		model.addAttribute("page", page);
		model.addAttribute("today", new Date());
		model.addAttribute("todayStr", Calendar.getInstance().getTime());
		
		return "test";
	}
	
	@RequestMapping(value="/detail")
	public String detail(Model model){
		model.addAttribute("show","detail");
		System.out.println("detail");
		return "detail";
	}
	
	@RequestMapping(value="/go/{id}")
	public String go(@PathVariable("id") String id,Model model){
		String rid = id;
		model.addAttribute("show","onlyid"+id);
		System.out.println("detail/go/"+id);
		return "detail";
	}
	
	@RequestMapping(value="/go/{id}/{name}")
	public String go(Model model,@PathVariable("id") String id,@PathVariable("name") String name){
		String rid = id;
		String rname = name;
		model.addAttribute("show","idname"+id+name);
		System.out.println("detail/go/"+id+"/"+name);
		return "detail";
	}
	
}
